package com.gaohe.service.Impl;

import cn.hutool.core.img.gif.NeuQuant;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gaohe.common.BaseContext;
import com.gaohe.mapper.ClassesMapper;
import com.gaohe.mapper.UserClassesMapper;
import com.gaohe.pojo.Classes;
import com.gaohe.pojo.User;
import com.gaohe.pojo.UserClasses;
import com.gaohe.service.ClassesService;
import com.gaohe.service.UserCardService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
@Slf4j
@Service
public class ClassesServiceImpl extends ServiceImpl<ClassesMapper, Classes> implements ClassesService {
    @Autowired
    private ClassesMapper classesMapper;
    @Autowired
    private UserClassesMapper userClassesMapper;
    @Override
    public List<Classes> getByUserId(Long userId) {
        List<Classes> classesList = classesMapper.getByUserId(userId);
        return classesList;
    }
    //根据课程 id 查用户
    @Override
    public List<User> getUserByClassesId(Long id) {
        List<User> userList = classesMapper.getUserByClassesId(id);
        return userList;
    }
    //用户端 根据标签查询课程
    @Override
    public List<String> getClassesByCardTags(String tags) {
        String[] tagsName = tags.split(",");
        LambdaQueryWrapper<Classes> qw = new LambdaQueryWrapper<>();
        qw.select(Classes::getName);
        for (String t : tagsName) {
            qw.like(Classes::getTags,t).or();
        }
        List<String> classesList = this.listObjs(qw);
        return classesList;
    }
    //用户查课
    @Override
    public List<Classes> getClassList(String tags) {
        //获取用户 id
        Long userId = BaseContext.getCurrentId();
        //查看用户已选 课程
        LambdaQueryWrapper<UserClasses> qwuc = new LambdaQueryWrapper<>();
        qwuc.select(UserClasses::getClassId);
        qwuc.eq(UserClasses::getUserId,userId);
        List<Long> classesIds = userClassesMapper.selectObjs(qwuc);
        //查询课程
        LambdaQueryWrapper<Classes> qw = new LambdaQueryWrapper<>();
        qw.like(Classes::getTags,tags);
        //排除 结课时间
        qw.ge(Classes::getEndTime, LocalDateTime.now());
        //排除用户已选课程
        qw.notIn(ObjectUtils.isNotEmpty(classesIds),Classes::getId,classesIds);
        List<Classes> classesList = this.list(qw);
        return classesList;
    }


}
